package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.DTO.GoodsOrderSelectDTO;
import com.example.demo.entity.Goods;
import com.example.demo.entity.GoodsOrders;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.GoodsOrdersMapper;
import com.example.demo.service.IGoodsOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Yang
 * @since 2024-05-31
 */
@Service
public class GoodsOrdersServiceImpl extends ServiceImpl<GoodsOrdersMapper, GoodsOrders> implements IGoodsOrdersService {

    @Autowired
    private GoodsOrdersMapper goodsOrdersMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<GoodsOrderSelectDTO> selectOrderByUserId(Integer userId) {
        //查询条件
        List<GoodsOrders> goodsOrders = goodsOrdersMapper.selectByUserId(userId);
        //装备一个list用来保存dto
        ArrayList<GoodsOrderSelectDTO> orderSelectDTOS = new ArrayList<>();
        //判断查询数据是否为空
        if(orderSelectDTOS!=null){
            //遍历结果
            for(GoodsOrders orders:goodsOrders){
                //new一个dto
                GoodsOrderSelectDTO orderSelectDTO = new GoodsOrderSelectDTO();
                //entity转dto
                BeanUtil.copyProperties(orders,orderSelectDTO);
                // 查询商品名称
                orderSelectDTO.setGoodsName(goodsMapper.selectById(orders.getGoodsId()).getGoodsName());
                //dto放入集合
                orderSelectDTOS.add(orderSelectDTO);
            }
        }
        return orderSelectDTOS;
    }

    @Override
    public List<GoodsOrders> selectAllOrder() {
        return goodsOrdersMapper.selectAllOrder();
    }

    @Override
    public Boolean updateOrderStatusById(GoodsOrders goods) {
        return goodsOrdersMapper.updateOrderStatusById(goods);
    }

    @Override
    public GoodsOrders findById(Integer id) {
        return goodsOrdersMapper.selectById(id);
    }

    @Override
    public Boolean deleteByGorderId(Integer id) {
        return goodsOrdersMapper.deleteByGorderId(id);
    }
}
